package com.whh.permission.controller;

import com.github.pagehelper.PageInfo;
import com.whh.permission.po.Asset;
import com.whh.permission.po.AssetScrap;
import com.whh.permission.po.AssetTake;
import com.whh.permission.po.User;
import com.whh.permission.service.IAssetHandlerService;
import com.whh.permission.util.JsonObject;
import com.whh.permission.util.R;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Controller
public class AssetHandlerController {
    @Resource
    private IAssetHandlerService assetHandlerServiceImpl;

    /**
     * 领用资产
     * @return
     */
    @RequestMapping("assetHandler/takeAsset")
    @ResponseBody
    public R takeAsset(HttpServletRequest request, Integer id){//添加领用表,改变资产状态
        User user = (User) request.getSession().getAttribute("user");
        assetHandlerServiceImpl.takeAsset(id,user.getId(),new Date());
        return R.ok();
    }

    /**
     * 归还资产
     */
    @RequestMapping("assetHandler/backAsset")
    @ResponseBody
    public R backAsset(Integer id,Integer aId){
        System.out.println(id+"---------"+aId);
        assetHandlerServiceImpl.backAsset(id,new Date(),aId);
        return R.ok();
    }

    /**
     * 报废资产
     */
    @RequestMapping("assetHandler/assetScrap")
    @ResponseBody
    public R scrapAsset(AssetScrap assetScrap){
        assetScrap.setCreateTime(new Date());
        assetHandlerServiceImpl.ScrapAsset(assetScrap);
        return R.ok();
    }

    /**
     * 报废资产修好了
     */
    @RequestMapping("assetHandler/assetScrapOk")
    @ResponseBody
    public R scrapAssetOk(AssetScrap assetScrap){
        assetScrap.setUpdateTime(new Date());
        assetScrap.setStatus(1);
        System.out.println(assetScrap);
        assetHandlerServiceImpl.ScrapAssetOk(assetScrap);
        return R.ok();
    }

    /**
     * 用户领用的资产查看
     * @param request
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("assetHandler/assetTakeByUId")
    @ResponseBody
    public JsonObject queryAssetInfoByStatus(HttpServletRequest request, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10")Integer limit){
        //获取uId
        User user = (User) request.getSession().getAttribute("user");
        Integer uId = user.getId();

        JsonObject object=new JsonObject();
        PageInfo<AssetTake> pageInfo=assetHandlerServiceImpl.queryAssetTakeByUId(page,limit,uId);
        object.setCode(0);
        object.setCount(pageInfo.getTotal());
        object.setData(pageInfo.getList());
        object.setMsg("ok");
        return object;
    }

    @RequestMapping("assetHandler/queryAssetScrapInfo")
    @ResponseBody
    public JsonObject queryAssetScrapInfo( @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10")Integer limit){

        JsonObject object=new JsonObject();
        PageInfo<AssetScrap> pageInfo=assetHandlerServiceImpl.queryAssetScrapAll(page,limit);
        object.setCode(0);
        object.setCount(pageInfo.getTotal());
        object.setData(pageInfo.getList());
        object.setMsg("ok");
        return object;
    }

    @RequestMapping("assetTake")
    public String assetTake(){
        return "pages/assetTake";
    }

    @RequestMapping("assetBack")
    public String assetBack(){
        return "pages/assetBack";
    }

    @RequestMapping("assetScrap")
    public String assetScrap(){
        return "pages/assetScrap";
    }

    @RequestMapping("addScrap")
    public String addScrap(Integer aId,Model model){
        model.addAttribute("aId",aId);
        return "pages/addScrap";
    }

    @RequestMapping("assetScrapInfo")
    public String assetScrapInfo(){
        return "pages/assetScrapInfo";
    }
}
